package com.lepu.app.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.app.config.AppConfig;
import com.app.utils.Const;
import com.app.utils.DspTongJi;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.eyzhs.app.R;

/*
 * 启动页
 */
public class AppStartActivity extends BaseFragmentActivity {

	public static final int DELAY_TIME = 1000 * 3;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedSwipeBack = false;
		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);

		View view = View.inflate(this, R.layout.app_start_activity, null);
		setContentView(view);

		//首发logo显示
		showLogo();
		LogUtilBase.LogD(null, "AppStartActivity=====>>onCreate");
		// 启动首页进入动画
		startAnim(view);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// 如果是启动页，屏蔽返回键
	}

	private void showLogo() {
		ImageView logoImageView = (ImageView) findViewById(R.id.logoImageView);

		String umengChannel = UtilityBase.getAppMetedata(this, "UMENG_CHANNEL");
		if (!TextUtils.isEmpty(umengChannel)
				&& umengChannel.equals("Baidu")) {
			logoImageView.setImageResource(R.drawable.app_logo_baidu);
			logoImageView.setVisibility(View.VISIBLE);
		}
//		else if (!TextUtils.isEmpty(umengChannel)
//				&& umengChannel.equals("MI")) {
//			logoImageView.setImageResource(R.drawable.app_logo_xiaomi);
//			logoImageView.setVisibility(View.VISIBLE);
//		}else if (!TextUtils.isEmpty(umengChannel)
//				&& umengChannel.equals("huawei")) {
//			logoImageView.setImageResource(R.drawable.app_logo_huawei);
//			logoImageView.setVisibility(View.VISIBLE);
//		}
		else {
			logoImageView.setVisibility(View.GONE);
		}
	}

	private void startAnim(View view) {

		AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f);
		alphaAnimation.setDuration(DELAY_TIME);
		view.startAnimation(alphaAnimation);
		alphaAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation arg0) {
				init();
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationStart(Animation animation) {
			}
		});
	}

	private void init() {
		// 进入程序逻辑判断
		boolean appFirstUse = AppConfig.getConfigBoolean(this,
				Const.CONFIG_APP_FIRST_USE, true);
//		boolean appUpgradeUse = AppConfig.getConfigBoolean(this,
//				Const.CONFIG_APP_UPGRADE_USE, true);
		
        //记录使用时间(有些用户已经安装,因此从此版本开始记录,不能从第一次安装记录)
		if(AppConfig.getConfigLong(this, Const.CONFIG_APP_USE_TIME, 0) == 0){
			AppConfig.setConfig(this, Const.CONFIG_APP_USE_TIME, DateUtilBase.getNowTimeInMillis());
		}
		
		Intent intent = new Intent();
		
		//第一次使用，或者程序升级也认为是第一次使用
		if (appFirstUse) {
			if (appFirstUse) {
				// 只需要dsp渠道,统计程序第一次使用
				new DspTongJi().requestFirstUse(AppStartActivity.this);
				
			}
			intent.setClass(AppStartActivity.this, AppStartActivityGuide.class);
		} else {

			intent.setClass(AppStartActivity.this, MainTabActivity.class);
		}
		// intent.setClass(AppStartActivity.this, MainTabActivity.class);
		startActivityAnim(intent,0,0);
		finishAnim(0,0);
	}
}
